package com.cuitrip.component;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.cuitrip.business.tripservice.ui.IconTextView;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.lab.utils.DisplayUtil;
import com.pnikosis.materialishprogress.ProgressWheel;

/**
 * Created by MiJack on 2015/11/30.
 */
public class PayPopupWindow extends PopupWindow {
    public enum STATE {LOADING, SUCCESS, FAILED}

    View iconClose;
    ProgressWheel progressBar;
    PayLoadingView payLoadingView;
    TextView contentTv;

    STATE state;

    public PayPopupWindow(View view) {
        super(view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        setFocusable(true);
        setOutsideTouchable(false);
        // 设置popWindow的显示和消失动画
        setAnimationStyle(R.style.popup_anim_style);
        iconClose = view.findViewById(R.id.iconClose);
        progressBar = (ProgressWheel) view.findViewById(R.id.progressBar);
        payLoadingView = (PayLoadingView) view.findViewById(R.id.payLoadingView);
        this.contentTv = (TextView) view.findViewById(R.id.content);
        iconClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PayPopupWindow.this.dismiss();
            }

        });
    }

    public STATE getState() {
        return state;
    }

    public void setState(STATE state) {
        this.state = state;
        switch (state) {
            case LOADING:
                iconClose.setVisibility(View.GONE);
                progressBar.setVisibility(View.VISIBLE);
                payLoadingView.setVisibility(View.GONE);
                contentTv.setVisibility(View.GONE);
                break;
            case SUCCESS:
                iconClose.setVisibility(View.GONE);
                progressBar.setVisibility(View.GONE);
                payLoadingView.setVisibility(View.VISIBLE);
                contentTv.setVisibility(View.GONE);
                payLoadingView.setStatus(PayLoadingView.State.SUCCESS);
                ObjectAnimator animator = ObjectAnimator.ofFloat(payLoadingView, "progess", 0f, 1f)
                        .setDuration(2000);
                animator.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        dismiss();
                        if (call != null) {
                            call.call();
                        }
                    }
                });
                animator.start();
                break;
        }
    }

    CallBack call;

    public interface CallBack {
        void call();
    }

    public void setCall(CallBack call) {
        this.call = call;
    }

    public void setFail(String c) {
        contentTv.setText(c);
        iconClose.setVisibility(View.VISIBLE);
        ObjectAnimator.ofFloat(progressBar, "alpha", 1f, 0f)
                .setDuration(2000).start();
        payLoadingView.setVisibility(View.VISIBLE);
        contentTv.setVisibility(View.VISIBLE);
        payLoadingView.setStatus(PayLoadingView.State.FAILED);
        ObjectAnimator animator = ObjectAnimator.ofFloat(payLoadingView, "progess", 0f, 1f)
                .setDuration(2000);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (call != null) {
                    call.call();
                }
            }
        });
        animator.start();
    }
}
